package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author hslef
 */
@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;

    @Override
    public PageInfo<Destination> query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Destination> listData = destinationMapper.selectForList(qo);
        return new PageInfo<>(listData);
    }

    @Override
    public List<Destination> getDestByDeep(Integer deep) {
        return destinationMapper.selectByDeep(deep);
    }

    @Override
    public void changeHotValue(int hot, Long id) {
        destinationMapper.updateIsHotByPrimaryKey(hot, id);
    }

    @Override
    public List<Destination> getToasts(Long parentId) {
        List<Destination> toasts = new ArrayList<>();
        createToasts(parentId, toasts);
        return toasts;
    }

    @Override
    public void setInfo(Long id, String info) {
        destinationMapper.updateInfoByPrimaryId(id, info);
    }

    @Override
    public void delete(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Destination getById(Long id) {
        return destinationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    @Override
    public Destination getProvince(Long id) {
        List<Destination> toasts = this.getToasts(id);
        if(toasts.size() >= 2 && toasts.get(0).getId() == 1){
            return toasts.get(1);
        }
        return null;
    }

    @Override
    public Destination getCountry(Long id) {
        return this.getToasts(id).get(0);
    }

    @Override
    public Destination getByName(String name) {
        return destinationMapper.selectByName(name);
    }


    private void createToasts(Long id, List toasts) {
        Destination destination = destinationMapper.selectByPrimaryKey(id);
        if (destination != null) {
            if (destination.getParent() != null) {
                createToasts(destination.getParent().getId(), toasts);
            }
            toasts.add(destination);
        }
    }

    //从数组中随机获取数据的方法
    public  List getRandomList(List paramList,int count){
        if(paramList.size()<count){
            return paramList;
        }
        Random random=new Random();
        List<Integer> tempList=new ArrayList<Integer>();
        List<Object> newList=new ArrayList<Object>();
        int temp=0;
        for(int i=0;i<count;i++){
            temp=random.nextInt(paramList.size());//将产生的随机数作为被抽list的索引
            if(!tempList.contains(temp)){
                tempList.add(temp);
                newList.add(paramList.get(temp));
            }
            else{
                i--;
            }
        }
        return newList;
    }

    public List<Destination> getCitys(Long id) {
        List<Destination> list = destinationMapper.selectByParentId(id);
        return list;
    }


}
